package com.cloud.mall.user.service.impl;

import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.model.user.AppUser;
import com.cloud.mall.model.utils.AppUserUtil;
import com.cloud.mall.ums.mapper.UmsMemberReceiveAddressMapper;
import com.cloud.mall.ums.model.UmsMember;
import com.cloud.mall.ums.model.UmsMemberReceiveAddress;
import com.cloud.mall.ums.model.UmsMemberReceiveAddressExample;
import com.cloud.mall.ums.model.UmsMenu;
import com.cloud.mall.user.input.UmsMemberReceiveAddressInpDTO;
import com.cloud.mall.user.output.UmsMemberReceiveAddressOut;
import com.cloud.mall.usr.service.UmsMemberReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private UmsMemberReceiveAddressMapper addressMapper;

    @Override
    public BaseResponse create(UmsMemberReceiveAddressInpDTO memberReceiveAddressInpDTO) {
        UmsMemberReceiveAddress address = MallBeanUtils.dtoToDo(memberReceiveAddressInpDTO,UmsMemberReceiveAddress.class);
        AppUser currentMember = AppUserUtil.getLoginAppUser();
        address.setMemberId(currentMember.getId());
        int count= addressMapper.insert(address);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse delete(Long id) {
        AppUser currentMember = AppUserUtil.getLoginAppUser();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        int count= addressMapper.deleteByExample(example);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id, UmsMemberReceiveAddressInpDTO memberReceiveAddressInpDTO) {
        UmsMemberReceiveAddress address = MallBeanUtils.dtoToDo(memberReceiveAddressInpDTO,UmsMemberReceiveAddress.class);
        address.setId(null);
        AppUser currentMember = AppUserUtil.getLoginAppUser();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        int count= addressMapper.updateByExampleSelective(address,example);
        if(count==0){
            return  BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse<List<UmsMemberReceiveAddressOut>> list() {
        AppUser currentMember = AppUserUtil.getLoginAppUser();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId());
        return BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(addressMapper.selectByExample(example),UmsMemberReceiveAddressOut.class));
    }

    @Override
    public BaseResponse<UmsMemberReceiveAddressOut> getItem(Long id) {
        AppUser currentMember = AppUserUtil.getLoginAppUser();
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andIdEqualTo(id);
        List<UmsMemberReceiveAddress> addressList = addressMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(addressList)){
            return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(addressList.get(0),UmsMemberReceiveAddressOut.class));
        }
        return  BaseResponse.setResultError();
    }
}
